//
// Created by hugan on 2020/12/5.
//

#ifndef JOYSTICKCPP_JOYSTICKJNA_CPP
#define JOYSTICKCPP_JOYSTICKJNA_CPP

#include "../joystick/JoystickManager.h"

#ifdef __cplusplus
extern "C" {
#endif
#define EXPORT __declspec(dllexport)

static JoystickManager joystickManager;//NOLINT,屏蔽警告

/**
 * dll版本号
 */
EXPORT const char *version() {
    return "201208";
}

/**
 * 初始化api
 * https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee416756(v=vs.85)
 * @param isUtf8 程序运行时的字符串编码:idea开发时是UTF8,window运行时是GBK;没找到cpp中直接获取运行时编码的函数,所以由java传参
 */
EXPORT HRESULT createDirectInput8(bool isUtf8, func_deviceStateCallback deviceStateCallback) {
    joystickManager.createDirectInput8(isUtf8, deviceStateCallback);
}

/**
 * 释放所有对象
 */
EXPORT void doFree() {
    joystickManager.release();
}

/**
 * 遍历设备;
 * https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee417804(v=vs.85)
 * @note 没有找到简单方法直接返回EnumDeviceData[],所以在java层实现返回list
 */
EXPORT HRESULT enumDevices(func_enumDevicesCallback callback) {
    joystickManager.enumDevices(callback);
}

/**
 * 开始监听设备按键;
 * .指令成功时,会阻塞当前线程;
 * .指令执行结果和轮询状态都在 func_deviceStateCallback中返回
 * https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee417897(v=vs.85)
 * @param guidString 来自 CommonUtil::getGUIDString
 * @param stateScanSpan 轮询间隔ms,如1000/n代表每秒最多返回n帧不同的数据
 * @param stickRange 摇杆的返回值范围=±stickRange,如1000
 * @param breakIfErr true-成功轮询监听后,如果出现错误就退出轮询;一般为false,则设备丢失后不用调用重连代码;
 */
EXPORT void startListenState(const char *guidString, int stateScanSpan, int stickRange, bool breakIfErr) {
    joystickManager.startListenState(guidString, stateScanSpan, stickRange, breakIfErr);
}

/**
 * 停止按键监听
 */
EXPORT void stopListenState() {
    joystickManager.stopListenState();
}

#ifdef __cplusplus
}
#endif
#endif //JOYSTICKCPP_JOYSTICKJNA_CPP